﻿' ----------------------------------------------------------------------------------
' Microsoft Developer & Platform Evangelism
' 
' Copyright (c) Microsoft Corporation. All rights reserved.
' 
' THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, 
' EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES 
' OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
' ----------------------------------------------------------------------------------
' The example companies, organizations, products, domain names,
' e-mail addresses, logos, people, places, and events depicted
' herein are fictitious.  No association with any real company,
' organization, product, domain name, email address, logo, person,
' places, or events is intended or should be inferred.
' ----------------------------------------------------------------------------------

Imports WindowsPhone.Recipes.Push.Messages
Imports Microsoft.Samples.WindowsPhoneCloud.Web.UserAccountWrappers
Imports Microsoft.Samples.WindowsPhoneCloud.Web.Models
Imports Microsoft.Samples.WindowsPhoneCloud.Web.Infrastructure
Imports System.Text
Imports System.Security.Cryptography.X509Certificates
Imports System.Security.Authentication
Imports System.Net.Sockets
Imports System.Net.Security
Imports System.IO
Imports System.Globalization
Imports System.Collections.Generic

Namespace Controllers

    <CustomAuthorize(Roles:=PrivilegeConstants.AdminPrivilege)> _
    Public Class PushNotificationsController
        Inherits Controller
        Private Const DefaultPort As Integer = 2195

        Private ReadOnly pushUserEndpointsRepository As IPushUserEndpointsRepository

#If ACS Then
        Private ReadOnly userRepository As IUserRepository

        Public Sub New()
            Me.New(Nothing, Nothing)
        End Sub

        <CLSCompliant(False)> _
        Public Sub New(ByVal pushUserEndpointsRepository As IPushUserEndpointsRepository, ByVal userRepository As IUserRepository)
            Dim context As SqlDataContext = Nothing
            If (pushUserEndpointsRepository Is Nothing) OrElse (userRepository Is Nothing) Then
                context = New SqlDataContext()
            End If

            Me.userRepository = If(userRepository, context)
            Me.pushUserEndpointsRepository = If(pushUserEndpointsRepository, context)
        End Sub

        Public Function Microsoft() As ActionResult
            Dim users = Me.pushUserEndpointsRepository.GetAllPushUsers().Select(Function(userId) New UserModel With {.UserId = userId, .UserName = Me.userRepository.GetUser(userId).Name})

            Return Me.View(users)
        End Function
#Else
        Private ReadOnly membershipService As IMembershipService

        Public Sub New()
            Me.New(New SqlDataContext(), New AccountMembershipService())
        End Sub

        <CLSCompliant(False)> _
        Public Sub New(ByVal pushUserEndpointsRepository As IPushUserEndpointsRepository, ByVal membershipService As IMembershipService)
            Me.membershipService = membershipService
            Me.pushUserEndpointsRepository = pushUserEndpointsRepository
        End Sub

        Public Function Microsoft() As ActionResult
            Dim users = Me.pushUserEndpointsRepository.GetAllPushUsers().Select(Function(userId) New UserModel With {.UserId = userId, .UserName = Me.membershipService.GetUserByProviderUserKey(New Guid(userId)).UserName})

            Return Me.View(users)
        End Function
#End If

        Public Function Apple() As ActionResult
            Return Me.View()
        End Function

        <HttpPost()> _
        Public Function SendMicrosoftToast(ByVal userId As String, ByVal message As String) As ActionResult
            If String.IsNullOrWhiteSpace(message) Then
                Return Me.Json("The notification message cannot be null, empty nor white space.", JsonRequestBehavior.AllowGet)
            End If

            Dim resultList = New List(Of MessageSendResultLight)()
            Dim uris = Me.pushUserEndpointsRepository.GetPushUsersByName(userId).Select(Function(u) u.ChannelUri)
            Dim toast = New ToastPushNotificationMessage With {.SendPriority = MessageSendPriority.High, .Title = message}

            For Each uri In uris
                Dim messageResult = toast.SendAndHandleErrors(New Uri(uri))
                resultList.Add(messageResult)
                If messageResult.Status.Equals(MessageSendResultLight.Success) Then
                    Me.QueueMessage(message, New Uri(uri))
                End If
            Next uri

            Return Me.Json(resultList, JsonRequestBehavior.AllowGet)
        End Function

        <HttpPost()> _
        Public Function SendMicrosoftTile(ByVal userId As String, ByVal message As String) As ActionResult
            If String.IsNullOrWhiteSpace(message) Then
                Return Me.Json("The notification message cannot be null, empty nor white space.", JsonRequestBehavior.AllowGet)
            End If

            Dim resultList = New List(Of MessageSendResultLight)()
            Dim pushUserEndpointList = Me.pushUserEndpointsRepository.GetPushUsersByName(userId)
            For Each pushUserEndpoint In pushUserEndpointList
                pushUserEndpoint.TileCount += 1
                Dim tile = New TilePushNotificationMessage With {.SendPriority = MessageSendPriority.High, .Count = pushUserEndpoint.TileCount}

                Dim messageResult = tile.SendAndHandleErrors(New Uri(pushUserEndpoint.ChannelUri))
                resultList.Add(messageResult)
                If messageResult.Status.Equals(MessageSendResultLight.Success) Then
                    Me.QueueMessage(message, New Uri(pushUserEndpoint.ChannelUri))

                    Me.pushUserEndpointsRepository.UpdatePushUserEndpoint(pushUserEndpoint)
                End If
            Next pushUserEndpoint

            Return Me.Json(resultList, JsonRequestBehavior.AllowGet)
        End Function

        <HttpPost()> _
        Public Function SendMicrosoftRaw(ByVal userId As String, ByVal message As String) As ActionResult
            If String.IsNullOrWhiteSpace(message) Then
                Return Me.Json("The notification message cannot be null, empty nor white space.", JsonRequestBehavior.AllowGet)
            End If

            Dim resultList = New List(Of MessageSendResultLight)()
            Dim uris = Me.pushUserEndpointsRepository.GetPushUsersByName(userId).Select(Function(u) u.ChannelUri)
            Dim raw = New RawPushNotificationMessage With {.SendPriority = MessageSendPriority.High, .RawData = Encoding.UTF8.GetBytes(message)}

            For Each uri In uris
                resultList.Add(raw.SendAndHandleErrors(New Uri(uri)))
            Next uri

            Return Me.Json(resultList, JsonRequestBehavior.AllowGet)
        End Function

        <HttpPost()> _
        Public Function SendAppleMessage(ByVal deviceId As String, ByVal message As String) As ActionResult
            If String.IsNullOrWhiteSpace(deviceId) Then
                Return Me.Json("The deviceId cannot be null, empty nor white space.", JsonRequestBehavior.AllowGet)
            End If

            If String.IsNullOrWhiteSpace(message) Then
                Return Me.Json("The notification message cannot be null, empty nor white space.", JsonRequestBehavior.AllowGet)
            End If

            ' Get the Apple Notification Service settings.
            Dim thumprint = ConfigReader.GetConfigValue("AppleNotificationService.Thumbprint")
            Dim host = ConfigReader.GetConfigValue("AppleNotificationService.Host")
            Dim port = 0
            If Not Integer.TryParse(ConfigReader.GetConfigValue("AppleNotificationService.Port"), NumberStyles.Integer, CultureInfo.InvariantCulture, port) Then
                port = DefaultPort
            End If

            Dim certCollection As X509Certificate2Collection = Nothing
            Try
                ' Load the Apple Notification Service certificate from the store.
                Dim clientCert = CertificateUtil.GetCertificate(StoreName.My, StoreLocation.LocalMachine, thumprint)
                certCollection = New X509Certificate2Collection(clientCert)
            Catch exception As Exception
                Dim errorMessage = String.Format(CultureInfo.InvariantCulture, "Error getting the X509 certificate from the store: {0}", exception.Message)
                Return Me.Json(errorMessage, JsonRequestBehavior.AllowGet)
            End Try

            Dim client As TcpClient = Nothing
            Try
                ' Open connection and connect.
                client = New TcpClient(host, port)
                Dim sslStream = New SslStream(client.GetStream(), False)

                sslStream.AuthenticateAsClient(host, certCollection, SslProtocols.Tls, False)

                Dim array() As Byte = Nothing
                Using memoryStream = New MemoryStream()
                    Dim writer = New BinaryWriter(memoryStream)

                    ' Construct the message.
                    writer.Write(CByte(0)) ' Command
                    writer.Write(CByte(0)) ' First byte of device ID length
                    writer.Write(CByte(32)) ' Device id length

                    ' Convert to hex and write to message.
                    Dim deviceToken(deviceId.Length \ 2 - 1) As Byte
                    For i As Integer = 0 To deviceToken.Length - 1
                        deviceToken(i) = Byte.Parse(deviceId.Substring(i * 2, 2), NumberStyles.HexNumber)
                    Next i

                    writer.Write(deviceToken)

                    ' Construct payload within JSON message framework.
                    Dim payload = String.Format(CultureInfo.InvariantCulture, "{{""aps"":{{""alert"":""{0}"",""badge"":1}}}}", message)

                    ' Write payload data.
                    writer.Write(CByte(0)) ' First byte of payload length
                    writer.Write(CByte(payload.Length)) ' Actual payload length
                    Dim b1() As Byte = Encoding.UTF8.GetBytes(payload)
                    writer.Write(b1)
                    writer.Flush()

                    array = memoryStream.ToArray()
                End Using

                ' Send across the wire.
                sslStream.Write(array)
                sslStream.Flush()
            Catch exception As AuthenticationException
                Dim errorMessage = String.Format(CultureInfo.InvariantCulture, "Error authenticating against APN: {0}", exception.Message)
                Return Me.Json(errorMessage, JsonRequestBehavior.AllowGet)
            Catch exception As Exception
                Dim errorMessage = String.Format(CultureInfo.InvariantCulture, "There was an error sending the notification message: {0}", exception.Message)
                Return Me.Json(errorMessage, JsonRequestBehavior.AllowGet)
            Finally
                ' Close the client connection.
                client.Close()
            End Try

            ' Success.
            Return Me.Json("Success", JsonRequestBehavior.AllowGet)
        End Function
        Private Sub QueueMessage(ByVal message As String, ByVal uri As Uri)
            Dim userEndpoint = Me.pushUserEndpointsRepository.GetPushUserEndpointByChannel(uri)
            If userEndpoint Is Nothing Then
                Throw New ArgumentException("The Channel URI is invalid", "uri")
            End If

            Me.pushUserEndpointsRepository.AddQueuedPushNotification(New QueuedPushNotification With {.PushUserEndpoint = userEndpoint, .ChannelUri = userEndpoint.ChannelUri, .Message = message})
        End Sub
    End Class
End Namespace